package com.blue.jdbc.config;

import com.blue.jdbc.JdbcObjectTemplate;
import com.blue.jdbc.dialect.MySQLDialect;
import org.springframework.beans.factory.config.RuntimeBeanReference;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.AbstractSimpleBeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import java.util.List;

/**
 * Template解析
 *
 * @author Jin Zheng
 * @date 2018-10-31
 */
public class TemplateParser extends AbstractSimpleBeanDefinitionParser
{
	public TemplateParser()
	{
	}

	@Override
	protected Class<?> getBeanClass(Element element)
	{
		return JdbcObjectTemplate.class;
	}

	@Override
	protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder)
	{
		try
		{
			String dataSource = element.getAttribute("data-source");
			builder.addPropertyValue("dataSource", new RuntimeBeanReference(dataSource));
			String dialect = element.getAttribute("dialect");
			builder.addPropertyValue("dialect", new RuntimeBeanReference(dialect));
			builder.addPropertyValue("debug", element.getAttribute("debug"));

			String machineIdProperties = element.getAttribute("machine-id-properties");
			if (machineIdProperties != null && !machineIdProperties.isEmpty())
			{
				builder.addPropertyValue("machineIdProperties", machineIdProperties);
			}
		}
		catch (Exception e)
		{
			parserContext.getReaderContext().error(MySQLDialect.class.getName() + " 无法创建", element, null, e);
		}
	}
}
